package com.welica.wat.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* @ClassName: ZookeeperLockConfig
* @Description: zookeeper分布式锁配置类
* @author weiyongpeng
* @date 2024年11月2日 下午4:37:29
 */
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@ConfigurationProperties(prefix = "distributed.lock.zookeeper")
public class ZookeeperLockConfig {
	
	/**
	 * zookeeper地址
	 */
	private String url;
	
	/**
	 * zookeeper锁节点根路径
	 */
	private String rootPath;
	
	/**
	 * 会话超时时间
	 */
	private int sessionTimeout = 30000;
	
	/**
	 * 链接超时时间
	 */
	private int connectTimeout = 15000;
	
	/**
	 * 最大重试间隔
	 */
	private int intervalRetryTime = 10000;
	
	/**
	 * 最大尝试次数
	 */
	private int maxRetries = 3;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public int getSessionTimeout() {
		return sessionTimeout;
	}

	public void setSessionTimeout(int sessionTimeout) {
		this.sessionTimeout = sessionTimeout;
	}

	public int getConnectTimeout() {
		return connectTimeout;
	}

	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
	}

	public int getMaxRetries() {
		return maxRetries;
	}

	public void setMaxRetries(int maxRetries) {
		this.maxRetries = maxRetries;
	}

	public int getIntervalRetryTime() {
		return intervalRetryTime;
	}

	public void setIntervalRetryTime(int intervalRetryTime) {
		this.intervalRetryTime = intervalRetryTime;
	}
	
	@Bean
    public CuratorFramework curatorFramework() {
        CuratorFramework client = CuratorFrameworkFactory.newClient(
                url,
                sessionTimeout,
                connectTimeout,
                new ExponentialBackoffRetry(intervalRetryTime, maxRetries)
        );
        client.start();
        return client;
    }
	
}
